package com.common.app.utls

import android.content.Context
import com.common.app.R
import com.common.app.utls.TrackUtils.track
import com.common.base.utils.AppUtils.getString
import org.json.JSONObject

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2024/6/18
 *
 * 描述：神策上报埋点
 *
 * 修订历史：
 *
 */
object StashArea {
    var location: String = "" // H5跳转商品详情页时传的跳转来源
    var shoppingCartSourceFlag: String = "" // 用于标记跳转到购物车来源

    fun reset() {
        location = ""
    }

    fun resetShoppingCartSourceFlag() {
        shoppingCartSourceFlag = ""
    }
}

object TrackUtils {
    const val TAG = "TrackUtil"
    const val CONFIG_UPLOAD_PATH = "CONFIG_UPLOAD_PATH"

    fun initConfig(context: Context, policyAgreed: Boolean = false) {
//        if (!policyAgreed) {
//            Log.i(TAG, "initConfig with policy not agreed")
//            return
//        }
//        val path = CentralManager.getInstance(IConfig::class.java)?.getConfig(CONFIG_UPLOAD_PATH)
//            ?: "https://scsdk.blackshark.com/sa?project=production"
//        //初始化配置
//        val saConfigOptions = SAConfigOptions(path)
//        // 开启全埋点
//        saConfigOptions
//            .setAutoTrackEventType(
//                SensorsAnalyticsAutoTrackEventType.APP_CLICK or
//                        SensorsAnalyticsAutoTrackEventType.APP_START or
//                        SensorsAnalyticsAutoTrackEventType.APP_END or
//                        SensorsAnalyticsAutoTrackEventType.APP_VIEW_SCREEN
//            )
//            .enableTrackPageLeave(true, true)
//            .enableJavaScriptBridge(true)//开启 H5埋点
//            .enableHeatMap(true)//开启点击分析功能
//            .enableTrackAppCrash()//开启Crash信息的自动采集
//            .enableLog(CentralManager.debuggable()) //开启 Log
//        // 需要在主线程初始化神策 SDK
//        SensorsDataAPI.startWithConfigOptions(context.applicationContext, saConfigOptions)
////        //添加公共参数
////        SensorsDataAPI.sharedInstance().registerDynamicSuperProperties {
////            return@registerDynamicSuperProperties null
////        }
//        //激活
//        SensorsDataAPI.sharedInstance().trackAppInstall()
    }



    fun login(loginId: String?, properties: JSONObject?) {
//        SensorsDataAPI.sharedInstance().login(loginId, properties)
    }

    fun getAnonymousID(): String {
        return "SensorsDataAPI.sharedInstance().anonymousId"
    }

    /**
     * 调用神策SDK上报埋点数据
     */
    fun track(eventName: String, properties: JSONObject? = null) {
//        SensorsDataAPI.sharedInstance().track(eventName, properties)
    }
}



//event id start
const val EVENT_HOME_PAGE_VIEW = "HomePageView"  //首页浏览
const val EVENT_HOME_SLIDE_SHOW_CLICK = "HomeSlideshowClick" //首页轮播图点击
const val EVENT_HOME_NAVIGATION_CLICK = "HomeNavigationIconClick" //首页金刚位点击
const val EVENT_HOME_COMMODITY_CLICK = "HomeCommodityClick"  //首页商品点击
const val EVENT_ACTIVITY_PAGE_VIEW = "ActivityPageView" //活动页浏览
const val EVENT_CATEGORY_PAGE_VIEW = "CategoryPageView" //分类页浏览
const val EVENT_CATEGORY_SIDEBAR_CLICK = "CategorySidebarClick" //分类侧边栏点击
const val EVENT_CATEGORY_COMMODITY_CLICK = "CategoryCommodityClick" //分类商品点击
const val EVENT_MINE_PAGE_VIEW = "MinePageView" //个人中心浏览
const val EVENT_MINE_CENTER_CLICK = "MineCenterClick" //个人中心点击
const val EVENT_SETTLE_CLICK = "SettleClick"
const val EVENT_SEARCH_BAR_CLICK = "SearchButtonClick" //搜索框点击
const val EVENT_SEARCH_START = "SearchStart" //搜索发起
const val EVENT_SEARCH_END = "SearchEnd" //搜索完成
const val EVENT_SEARCH_RESULT_CLICK = "SearchResultClick" //搜索结果点击
const val EVENT_COMMODITY_DETAIL_PAGE_VIEW = "CommodityDetailPageView" //商品详情页浏览
const val EVENT_COMMODITY_DETAIL_PAGE_CLICK = "CommodityDetailPageClick" //商品详情页点击
const val EVENT_COMMODITY_SPECIFICATIONS_CHOOSE = "CommoditySpecificationsChoose" //商品规格选择
const val EVENT_SHOPPING_CART_PAGE_VIEW = "AddCartPageView" //购物车页浏览
const val EVENT_SHOPPING_CART_BUY_NOW = "BuyNow" //立即购买
const val EVENT_SHOPPING_CART_ADD = "AddCart" //加入购物车
//event id end

//field start
const val FIELD_MODULE_INNER_RANK = "module_inner_rank" //模块内排序
const val FIELD_CLICK_CONTENT = "click_content" //点击内容
const val FIELD_FORWARD_TYPE = "forward_type" //跳转类型
const val FIELD_FORWARD_URL = "forward_url" //跳转链接

const val FIELD_BELONG_MODULE = "belong_module" //所属模块
const val FIELD_COMMODITY_SPU_ID = "commodity_spu_id" //商品 SPU ID
const val FIELD_COMMODITY_NAME = "commodity_name" //商品名称
const val FIELD_COMMODITY_CURRENT_PRICE = "commodity_current_price" //商品现价

const val FIELD_ACTIVITY_ID = "activity_id" //活动ID
const val FIELD_CURRENT_PAGE_NAME = "current_page_name" //当前页面名字

const val FIELD_COMMODITY_SKU_CODE_LIST = "commodity_sku_code_list" //商品SKU码列表
const val FIELD_COMMODITY_COUNT = "commodity_count" //商品购买数量
const val FIELD_COMMODITY_TOTAL_AMOUNT = "commodity_total_amount" //商品总金额
const val FIELD_SEARCH_KEYWORD_TYPE = "search_keyword_type" // 搜索关键词类型
const val FIELD_SEARCH_KEYWORD_CONTENT = "search_keyword_content" // 搜索关键词内容
const val FIELD_SEARCH_IS_SEARCH_MATCH = "is_search_match" // 是否搜索匹配成功
const val FIELD_SEARCH_RESULT_NUM = "search_result_num" // 搜索结果数量
const val FIELD_SEARCH_RESULT_RANK = "result_rank" // 搜索结果数量
const val FIELD_ANTECEDENT_PAGE = "antecedent_page" //前项来源页面
const val FIELD_COMMODITY_SKU_CODE = "commodity_sku_code" //商品 SKU码
//field end

//获取跳转类型对应的中文名称, for 神策埋点
fun getForwardStrByValue(type: String?): String =
    when (type) {
        "url" -> getString(R.string.bsstore_forward_type_url)
        "page" -> getString(R.string.bsstore_forward_type_page)
        "group" -> getString(R.string.bsstore_forward_type_group)
        "special" -> getString(R.string.bsstore_forward_type_special)
        "product" -> getString(R.string.bsstore_forward_type_product)
        "openBySystem" -> getString(R.string.bsstore_forward_type_openBySystem)
        "deeplink" -> getString(R.string.bsstore_forward_type_deeplink)
        else -> ""
    }


//活动页浏览事件
fun onActivityPageExposure(id: String, name: String) {
    var activityName = when(name) {
        //秒杀
        "seckill" -> getString(R.string.bsstore_activity_secondkill)

        //专题活动页
        "special" -> getString(R.string.bsstore_activity)

        else -> ""
    }
    val properties = JSONObject()
    try {
        properties.putOpt(FIELD_ACTIVITY_ID, id)
        properties.putOpt(FIELD_CURRENT_PAGE_NAME, activityName)
    } catch (e: Exception) {
        properties
    }
    track(EVENT_ACTIVITY_PAGE_VIEW, properties)
}